package com.qf.service.impl;

import com.qf.mapper.UserMapper;
import com.qf.pojo.User;
import com.qf.service.UserService;
import com.qf.utils.ResultMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;

    @Override
    public ResultMsg queryAllUser(String name, Integer roleId, Integer status, Integer page, Integer limit) {
        page = page <= 1 ? 1 : page;
        name = name.equals("") ? null : name;
        roleId = roleId == -1 ? null : roleId;
        status = status == -1 ? null : status;
        List<User> users = userMapper.queryAllUser(name, roleId, status, (page - 1) * limit, limit);
        if (users.size() == 0) return ResultMsg.FAILED(201, "no data was found");
        return ResultMsg.SUCCESS(0, users, userMapper.queryUserCount().getUserId());
    }

    @Override
    public ResultMsg queryUser(Integer userId, String username, String password, HttpServletRequest request) {
        if (userId == null && (username == null || password == null))
            return ResultMsg.FAILED(202, "parameter can not be null");
        List<User> users = userMapper.queryUser(userId, username, password);
        if (users.size() == 0) return ResultMsg.FAILED(201, "no data was found");
        if (userId == null && username != null && password != null) {
            HttpSession session = request.getSession();
            session.setAttribute("loginUser", users.get(0));
        }
        return ResultMsg.SUCCESS(users.get(0));
    }

    @Override
    public ResultMsg updateUser(User user) {
        if (user.getUserId() == null) return ResultMsg.FAILED(202, "parameter can not be null");
        List<User> users = userMapper.queryUser(null, user.getUserUsername(), null);
        if (users.size() > 0) {
            if (users.get(0).getUserId() != user.getUserId())
                return ResultMsg.FAILED(205, "username is exists");
        }
        try {
            int i = userMapper.updateUser(user);
            if (i == 0) return ResultMsg.FAILED(201, "failed to modify");
            return ResultMsg.SUCCESS();
        } catch (Exception e) {
            return ResultMsg.FAILED(203, e.getMessage());
        }
    }

    @Override
    public ResultMsg insertUser(User user) {
        List<User> users = userMapper.queryUser(null, user.getUserUsername(), null);
        if (users.size() > 0) return ResultMsg.FAILED(205, "username is exists");
        try {
            int i = userMapper.insertUser(user);
            if (i == 0) return ResultMsg.FAILED(201, "failed to modify");
            return ResultMsg.SUCCESS(user);
        } catch (Exception e) {
            return ResultMsg.FAILED(203, e.getMessage());
        }
    }

    @Override
    public ResultMsg updateUserStatus(List<Integer> userIds, Integer userStatus) {
        if (userStatus != 1 && userStatus != 2)
            return ResultMsg.FAILED(206, "The userStatus parameter does not allow this value");
        int i = userMapper.updateUserStatus(userIds, userStatus);
        return new ResultMsg(0, i + " rows of data are successfully modified", null, 0);
    }

    @Override
    public ResultMsg deleteUser(List<Integer> userIds) {
        int i = userMapper.deleteUser(userIds);
        if (i > 1)
            return new ResultMsg(0, i + " rows of data are deleted successfully", null, 0);
        else
            return new ResultMsg(0, i + " row of data is deleted successfully", null, 0);
    }

    @Override
    public ResultMsg logout(HttpServletRequest request) {
        try {
            request.getSession().setAttribute("loginUser", null);
            return ResultMsg.SUCCESS();
        } catch (Exception e) {
            return ResultMsg.FAILED(201, "退出失败");
        }
    }
}
